package ch.ethz.eai.handler;

import java.util.Iterator;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.handlers.AbstractHandler;

public class OutHandler extends AbstractHandler{

	@Override
	public InvocationResponse invoke(MessageContext mc) throws AxisFault {
		SOAPEnvelope envelope = mc.getEnvelope();
		SOAPBody body = envelope.getBody();
		Iterator i = body.getFirstElement().getChildElements();
		while (i.hasNext())
		{
			OMElement el = (OMElement) i.next();
			Iterator it = el.getChildElements();
			while(it.hasNext())
			{
				OMElement elem = (OMElement) it.next();
				if (elem.getLocalName().equals("weatherStatus"))
				{
					String msg = elem.getText();
					if (msg.equals("-2"))
						elem.setText("undefinded");
					else if (msg.equals("-1"))
						elem.setText("unknown");
					else if (msg.equals("0"))
						elem.setText("clear");
					else if (msg.equals("1"))
						elem.setText("cloudy");
					else if (msg.equals("2"))
						elem.setText("rainy");
					else if (msg.equals("3"))
						elem.setText("stormy");
					else if (msg.equals("4"))
						elem.setText("snowy");
					break;
				}
			}
		}
		return InvocationResponse.CONTINUE;
	}

}
